/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.william;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.DatabaseFactory;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.serverpackets.S_NpcChatPacket;
import l1j.server.server.utils.Random;
import l1j.server.server.utils.SQLUtil;

public class MobTalk {

	private static final Logger _log = Logger
			.getLogger(MobTalk.class.getName());

	private static MobTalk _instance;

	public static MobTalk getInstance() {
		if (_instance == null) {
			_instance = new MobTalk();
		}
		return _instance;
	}

	private final HashMap<Integer, MobTalkTemp> _talkList = new HashMap<Integer, MobTalkTemp>();

	public MobTalk() {
		this.LoadData();
	}

	public void forL1MonsterTalking(final L1NpcInstance monster, final int type) {
		if (monster == null) {
			return;
		}
		final MobTalkTemp temp = this._talkList.get(monster.getNpcId());
		if (temp == null) {
			return;
		}
		if (temp.getType() != type) {
			return;
		}

		if (monster.hasSkillEffect(6019)) {
			return;
		}

		final int chance = Random.nextInt(100) + 1;

		if (temp.getRandom() >= chance) {

			if (temp.getShout() != 0) {// 大喊

				monster.broadcastPacketAll(new S_NpcChatPacket(monster, temp
						.getTalk(), 2));
			} else {
				monster.broadcastPacketAll(new S_NpcChatPacket(monster, temp
						.getTalk(), 0));
			}
			if (temp.getDelay() > 0) { // 說話延遲
				monster.setSkillEffect(6019, temp.getDelay() * 1000);
			} else {
				monster.setSkillEffect(6019, 1000);
			}
		}
	}

	private void LoadData() {
		Connection con = null;
		Statement stat = null;
		ResultSet rset = null;
		try {
			con = DatabaseFactory.get().getConnection();
			stat = con.createStatement();
			rset = stat.executeQuery("SELECT * FROM william_mob_talk");
			while (rset.next()) {
				final MobTalkTemp talk = new MobTalkTemp();
				talk.setType(rset.getInt("type"));
				talk.setTalk(rset.getString("talk"));
				talk.setDelay(rset.getInt("delay"));
				talk.setShout(rset.getInt("shout"));
				talk.setRandom(rset.getInt("random"));
				this._talkList.put(rset.getInt("mob_id"), talk);
			}
		} catch (final Exception ex) {
			_log.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
		} finally {
			SQLUtil.close(rset, stat, con);
		}
	}
	/*
	 * //NPC說話順序判斷 private int _order = 0;
	 * 
	 * public int getOrder() { return _order; }
	 * 
	 * public void setOrder(final int i) { _order = i; } // NPC說話順序判斷
	 */
}
